package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.Goods;
import com.jz.jd.rest.response.HotGoodsResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.ImageLoaderHelper;
import jz.com.feierman.R;


/**
 * Created by Administrator on 2018/3/19.
 */

public class SearchListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    @BindView(R.id.btn_search)
    EditText btn_search;

    int page = 1;
    String searchText;

    MyAdapter myAdapter = new MyAdapter();
    List<Goods> list = new ArrayList<>();

    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 10;
                outRect.right = 10;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);

        btn_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == event.KEYCODE_ENTER) {
                    if(btn_search.getText().toString().trim().equals("")){
                        Toast.makeText(SearchListActivity.this,"请输入查询关键字",Toast.LENGTH_SHORT).show();
                        return false;
                    }
                    searchText = btn_search.getText().toString().trim();
                    page = 1;
                    getHotGoods(1,9,searchText);
                }
                return false;
            }
        });
        btn_search.setText(searchText);
    }

    @Override
    protected void initData() {
        searchText = getIntent().getStringExtra("searchText");
        page = 1;
        getHotGoods(1,9,searchText);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_search_result;
    }

    @Override
    public void onLoadMore() {
        page++;
        getHotGoods(page, 9,searchText);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getHotGoods(1,9,searchText);
    }
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_hot, parent, false));
            return holder;
        }


        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("goodsid",list.get(position).goods_id);
                    startActivity(GoodsDetailActivity.class,bundle);
                }
            });
            ImageLoaderHelper.getInstance().GlideImageLoader(SearchListActivity.this,list.get(position).getGoods_imgs(),holder.img_goods);
            holder.tv_price.setText("¥ "+list.get(position).goods_price);
            holder.tv_name.setText(list.get(position).goods_name);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_name, tv_price;
            ImageView img_goods;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_price = itemView.findViewById(R.id.tv_price);
                img_goods = itemView.findViewById(R.id.img_goods);
            }
        }
    }


    private void getHotGoods(final int page, int pageSize,String searchText){
        RestProxy.getInstance().getHotGoods(page,pageSize,searchText,new Observer<HotGoodsResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(HotGoodsResponse hotGoodsResponse) {
                if(page == 1){
                    list.clear();
                }
                list.addAll(hotGoodsResponse.listM);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }
}
